//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "KlugeExtOUProcess.h"
using namespace Cephei::QL::Experimental::Processes;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Experimental/Processes/ExtendedOrnsteinUhlenbeckProcess.h>
#include <gen/QL/Experimental/Processes/ExtOUWithJumpsProcess.h>
#include <gen/QL/StochasticProcess.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Processes::CKlugeExtOUProcess::CKlugeExtOUProcess (Double rho, Cephei::QL::Experimental::Processes::IExtOUWithJumpsProcess^ kluge, Cephei::QL::Experimental::Processes::IExtendedOrnsteinUhlenbeckProcess^ extOU) : CStochasticProcess(CKlugeExtOUProcess::typeid)
{
    CExtOUWithJumpsProcess^ _Ckluge;
    CExtendedOrnsteinUhlenbeckProcess^ _CextOU;
    try
    {
#ifdef HANDLE
        _phKlugeExtOUProcess = NULL;
#endif
        QuantLib::Real _rho = (QuantLib::Real)ValueHelper::Convert (rho); //d
        _Ckluge = safe_cast<CExtOUWithJumpsProcess^> (kluge);
        _Ckluge->Lock();
        boost::shared_ptr<QuantLib::ExtOUWithJumpsProcess>& _kluge = static_cast<boost::shared_ptr<QuantLib::ExtOUWithJumpsProcess>&> (_Ckluge->GetShared ()); 
        _CextOU = safe_cast<CExtendedOrnsteinUhlenbeckProcess^> (extOU);
        _CextOU->Lock();
        boost::shared_ptr<QuantLib::ExtendedOrnsteinUhlenbeckProcess>& _extOU = static_cast<boost::shared_ptr<QuantLib::ExtendedOrnsteinUhlenbeckProcess>&> (_CextOU->GetShared ()); 
        _ppKlugeExtOUProcess = new boost::shared_ptr<QuantLib::KlugeExtOUProcess> (new QuantLib::KlugeExtOUProcess ( _rho,  _kluge,  _extOU ));
        SetStochasticProcess (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppKlugeExtOUProcess));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ckluge != nullptr) _Ckluge->Unlock();
        if (_CextOU != nullptr) _CextOU->Unlock();
    }
}
Cephei::QL::Experimental::Processes::CKlugeExtOUProcess::CKlugeExtOUProcess (boost::shared_ptr<QuantLib::KlugeExtOUProcess>& childNative, Object^ owner) : CStochasticProcess(CKlugeExtOUProcess::typeid)
{
#ifdef HANDLE
	_phKlugeExtOUProcess = NULL;
#endif
	_ppKlugeExtOUProcess = &childNative;
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppKlugeExtOUProcess));
}
Cephei::QL::Experimental::Processes::CKlugeExtOUProcess::CKlugeExtOUProcess (QuantLib::KlugeExtOUProcess& childNative, Object^ owner) : CStochasticProcess(CKlugeExtOUProcess::typeid)
{
#ifdef HANDLE
	_phKlugeExtOUProcess = NULL;
#endif
	_ppKlugeExtOUProcess = new boost::shared_ptr<QuantLib::KlugeExtOUProcess> (&childNative);
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppKlugeExtOUProcess));
    _KlugeExtOUProcessOwner = owner;
    _StochasticProcessOwner = owner;
}

Cephei::QL::Experimental::Processes::CKlugeExtOUProcess::CKlugeExtOUProcess (CKlugeExtOUProcess^ copy) : CStochasticProcess(CKlugeExtOUProcess::typeid)
{
#ifdef HANDLE
	_phKlugeExtOUProcess = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppKlugeExtOUProcess = new boost::shared_ptr<QuantLib::KlugeExtOUProcess> (copy->GetShared());
        _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppKlugeExtOUProcess));
    }
}
Cephei::QL::Experimental::Processes::CKlugeExtOUProcess::CKlugeExtOUProcess (PLATFORM::Type^ t) : CStochasticProcess(CKlugeExtOUProcess::typeid)
{
#ifdef HANDLE
	_phKlugeExtOUProcess = NULL;
#endif
	if (!t->IsSubclassOf(CKlugeExtOUProcess::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Processes::CKlugeExtOUProcess::CKlugeExtOUProcess (QuantLib::Handle<QuantLib::KlugeExtOUProcess>& childNative, Object^ owner)  : CStochasticProcess(CKlugeExtOUProcess::typeid)
{
	_phKlugeExtOUProcess = &childNative;
	_ppKlugeExtOUProcess = &static_cast<boost::shared_ptr<QuantLib::KlugeExtOUProcess>>(childNative.currentLink());
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppKlugeExtOUProcess));
    _KlugeExtOUProcessOwner = owner;
}
Cephei::QL::Experimental::Processes::CKlugeExtOUProcess::CKlugeExtOUProcess (QuantLib::Handle<QuantLib::KlugeExtOUProcess> childNative)  : CStochasticProcess(CKlugeExtOUProcess::typeid)
{
	_phKlugeExtOUProcess = &childNative;
	_ppKlugeExtOUProcess = &static_cast<boost::shared_ptr<QuantLib::KlugeExtOUProcess>>(childNative.currentLink());
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppKlugeExtOUProcess));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Processes::CKlugeExtOUProcess::CKlugeExtOUProcess (QuantLib::KlugeExtOUProcess childNative)  : CStochasticProcess(CKlugeExtOUProcess::typeid)
{
#ifdef HANDLE
	_phKlugeExtOUProcess = NULL;
#endif
	_ppKlugeExtOUProcess = new boost::shared_ptr<QuantLib::KlugeExtOUProcess> (new QuantLib::KlugeExtOUProcess (childNative));
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppKlugeExtOUProcess));
}
#endif

Cephei::QL::Experimental::Processes::CKlugeExtOUProcess::~CKlugeExtOUProcess ()
{
    if (_ppKlugeExtOUProcess != NULL)
    {
	    delete _ppKlugeExtOUProcess;
        _ppKlugeExtOUProcess = NULL;
    }
}
Cephei::QL::Experimental::Processes::CKlugeExtOUProcess::!CKlugeExtOUProcess ()
{
    if (_ppKlugeExtOUProcess != NULL)
    {
	    delete _ppKlugeExtOUProcess;
    }
}
QuantLib::KlugeExtOUProcess& Cephei::QL::Experimental::Processes::CKlugeExtOUProcess::GetReference ()
{
    if (_ppKlugeExtOUProcess == NULL) throw REFNEW NativeNullException ();
	return **_ppKlugeExtOUProcess;
}
boost::shared_ptr<QuantLib::KlugeExtOUProcess>& Cephei::QL::Experimental::Processes::CKlugeExtOUProcess::GetShared ()
{
    if (_ppKlugeExtOUProcess == NULL) throw REFNEW NativeNullException ();
	return *_ppKlugeExtOUProcess;
}
QuantLib::KlugeExtOUProcess* Cephei::QL::Experimental::Processes::CKlugeExtOUProcess::GetPointer ()
{
    if (_ppKlugeExtOUProcess == NULL) throw REFNEW NativeNullException ();
	return &**_ppKlugeExtOUProcess;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::KlugeExtOUProcess>& Cephei::QL::Experimental::Processes::CKlugeExtOUProcess::GetHandle ()
{
	if (_phKlugeExtOUProcess == NULL)
	{
		_phKlugeExtOUProcess = new Handle<QuantLib::KlugeExtOUProcess> (*_ppKlugeExtOUProcess);
	}
	return *_phKlugeExtOUProcess;
}
#endif
bool Cephei::QL::Experimental::Processes::CKlugeExtOUProcess::HasNative () 
{
	return (_ppKlugeExtOUProcess != NULL);
}

UInt64 Cephei::QL::Experimental::Processes::CKlugeExtOUProcess::Factors::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppKlugeExtOUProcess)->factors ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Processes::IExtendedOrnsteinUhlenbeckProcess^ Cephei::QL::Experimental::Processes::CKlugeExtOUProcess::GetExtOUProcess::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::ExtendedOrnsteinUhlenbeckProcess> _rv = (boost::shared_ptr<QuantLib::ExtendedOrnsteinUhlenbeckProcess>)(*_ppKlugeExtOUProcess)->getExtOUProcess ( );   
        Cephei::QL::Experimental::Processes::CExtendedOrnsteinUhlenbeckProcess^ _nrv = REFNEW Cephei::QL::Experimental::Processes::CExtendedOrnsteinUhlenbeckProcess (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Processes::IExtOUWithJumpsProcess^ Cephei::QL::Experimental::Processes::CKlugeExtOUProcess::GetKlugeProcess::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::ExtOUWithJumpsProcess> _rv = (boost::shared_ptr<QuantLib::ExtOUWithJumpsProcess>)(*_ppKlugeExtOUProcess)->getKlugeProcess ( );   
        Cephei::QL::Experimental::Processes::CExtOUWithJumpsProcess^ _nrv = REFNEW Cephei::QL::Experimental::Processes::CExtOUWithJumpsProcess (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Processes::CKlugeExtOUProcess::Rho::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppKlugeExtOUProcess)->rho ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Experimental::Processes::CKlugeExtOUProcess::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppKlugeExtOUProcess)->size ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Processes::IKlugeExtOUProcess^ Cephei::QL::Experimental::Processes::CKlugeExtOUProcess_Factory::Create (Double rho, Cephei::QL::Experimental::Processes::IExtOUWithJumpsProcess^ kluge, Cephei::QL::Experimental::Processes::IExtendedOrnsteinUhlenbeckProcess^ extOU)
{
    return REFNEW CKlugeExtOUProcess ( rho,  kluge,  extOU);
}
